<!--我们的注册界面-->
<template>
  <div class="backgroud">
    <div class="register">
      <el-form style="width: 100% ;height: 100%" :model="user" :rules="rules" ref="registerRef" v-loading="loading">
        <div style="margin-top: 72px; margin-left: 50px;font-size:27px;font-weight: bolder">
          欢迎注册
        </div>
        <div style="display: flex;align-items: center; justify-content: center; margin-top: 37px">
          <el-form-item prop="name">
            <!--                账号输入框-->
            <span style="font-size: 16px;font-weight: bolder">用户名：</span>
            <el-input placeholder="请输入用户名" v-model="user.name" size="120px" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
        </div>
        <div style="display: flex;align-items: center; justify-content: center; margin-top: 5px">
          <el-form-item prop="password">
            <!--                密码输入框-->
            <span style="font-size: 16px;font-weight: bolder">密码：</span>
            <el-input placeholder="请输入密码" size="120px" v-model="user.password" show-password
                      prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
        </div>

        <div style="display: flex;align-items: center; justify-content: center; margin-top: 5px">
          <el-form-item prop="checkPassword">
            <!--                确认密码输入框-->
            <span style="font-size: 16px;font-weight: bolder">确认密码：</span>
            <el-input placeholder="确认密码" size="120px" v-model="checkPassword" show-password
                      prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
        </div>

        <div style="display: flex;align-items: center; justify-content: center; margin-top: 5px">
          <el-form-item prop="phone">
            <!--                手机号-->
            <span style="font-size: 16px;font-weight: bolder">手机号码：</span>
            <el-input placeholder="请输入手机号" v-model="user.phone" size="120px" prefix-icon="el-icon-phone"></el-input>
          </el-form-item>
        </div>

        <div style="display: flex;align-items: center;justify-content: center;margin-top: 5px">
          <el-form-item style="margin-top: 27px">
            <!--                登录按钮-->
            <el-button type="primary" style="width: 100%;height: 42px;font-size: 16px;" @click="register"
                       :disabled="disabled">
              注&nbsp&nbsp&nbsp&nbsp&nbsp册
            </el-button>

          </el-form-item>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    var validName = (rule, value, callback) => {
      if (value === '') {
        this.disabled = true
        callback(new Error('请输入注册的用户名'))
      }else if (value.length>20){
        this.disabled = true
        callback(new Error('用户名不能超过20个字符'))
      }
      else {
        this.disabled = false
        callback();
      }
    }
    var validPassword = (rule, value, callback) => {
      if (value === '') {
        this.disabled = true
        callback(new Error('请输入注册的密码'))
      } else if (!this.checkPasswordMethod()) {
        //用户名已经被注册了
        this.disabled = true
        callback(new Error("前后密码不正确"))
      } else {
        this.disabled = false
        callback();
      }
    }
    var validPhone = (rule, value, callback) => {
      //校验中国电话号码的正则表达式
      const chinesePhoneNumberRegex = /^1[3456789]\d{9}$/;
      if (value === '') {
        this.disabled = true
        callback(new Error('请输入手机号'))
      } else if (!chinesePhoneNumberRegex.test(value)) {
        this.disabled = true
        callback(new Error('请输入正确的手机号！'))
      } else {
        this.disabled = false
        callback();
      }
    }

    return {
      user: {
        name: '', //用户名
        password: '', //密码
        phone: '', //电话
        loginType: '', //登录方式
        code: ''//电话验证码或者账号登录时用户输入的验证码
      },
      //注册的表单校验
      rules: {
        name: [
          {validator: validName, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        checkPassword: [
          {validator: validPassword, trigger: 'blur'}
        ],
        phone: [
          {validator: validPhone, trigger: 'blur'}
        ]
      },
      checkPassword: '',
      loading: false,
      disabled: false,
      checkFlag: ''
    }
  },
  methods: {
    register() {
      //  注册账号
      this.$refs["registerRef"].validate((valid) => {
        //表单验证通过且验证码正确才发送登录请求给后端
        if (valid) {
          this.loading = true
            this.$request.post('/login/register', this.user).then(res => {
              if (res.successFlag) {
                this.$router.push('/login')
                this.$message.success("注册成功！")
              } else {
                this.loading = false
                this.$notify({
                  title: '注册提示：',
                  message: "用户名或手机号已注册，请重新填写！",
                  type: 'error'
                })
                this.$message.error("注册失败！请检查注册信息是否有误！")

              }
            })
        }
      })


    },

    checkUserNameOrPhone() {
      //  检验用户名是否已经注册
      this.$request.post('/login/checkUserNameOrPhone', this.user).then(res => {
        if (!res) {
          this.$notify({
            title: '注册提示：',
            message: "用户名或手机号已注册，请重新填写！",
            type: 'error'
          })
        }
        this.checkFlag = res
      })
    },

    checkPasswordMethod() {
      if (this.checkPassword != this.user.password) {
        return false;
      } else {
        return true;
      }
    }
  }

}
</script>

<style scoped>
.backgroud {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('../assets/register_bg.png');
  background-size: 100% 100%;
}

.register {
  /*毛玻璃效果*/
  background-color: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  display: flex;
  height: 700px;
  width: 600px;
  border-radius: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}
</style>